from PySide6.QtWidgets import (QWidget,QVBoxLayout,QHBoxLayout,QCheckBox,QRadioButton,QGroupBox,QSystemTrayIcon,QMenu)
from PySide6.QtGui import QIcon
from windows_regedit import WinSystemThem
from one_day import OneDay


class AutoDark(QWidget):
    """自动切换主题
    """
    def __init__(self):
        """初始化"""
        super().__init__()
        self.setup_ui()
        self.setup_system_tray()
        self.set_event_bind()
        self.one_day = OneDay()
        self.one_day.load_config()
    
    def setup_ui(self):
        """设置界面"""
        # 窗口设置 标题和大小
        self.setWindowTitle("AutoDark") 
        self.setWindowIcon(QIcon(":/images/autoDark.png"))
        self.resize(300,140)

        ####  窗口主布局使用水平布局
        global_hbox = QHBoxLayout(self)

        #-- 勾选框 使用垂直布局 -- 添加开机自启动、主动选择主题勾选框
        check_vbox = QVBoxLayout()

        self.__enable_checkbox = QCheckBox("开机自启动(等待实现)")
        self.__auto_checkbox = QCheckBox("自动选择主题")
        
        check_vbox.addWidget(self.__enable_checkbox)
        check_vbox.addWidget(self.__auto_checkbox)

        #-- 主题组合框 使用垂直布局 -- 添加暗色和亮色 圆角按钮 默认读取注册表设置
        them_group_box = QGroupBox(title="主题选择")
        
        group_box = QVBoxLayout(them_group_box)

        self.light_radio_btn = QRadioButton("亮色")
        self.dark_radio_btn = QRadioButton("暗色")
        
        self.set_radio_btn_choosed()
        

        group_box.addWidget(self.light_radio_btn)
        group_box.addWidget(self.dark_radio_btn)

        # 主布局添加分布局或控件
        global_hbox.addLayout(check_vbox)
        global_hbox.addWidget(them_group_box)

    def setup_system_tray(self):
        """设置系统托盘"""
        # 设置托盘图标
        system_tray = QSystemTrayIcon(self)
        system_tray.setIcon(QIcon(":/images/autoDark.png"))
        system_tray.setToolTip("autoDark")

        # 设置托盘上下文菜单
        tray_menu = QMenu()
        self.__change = tray_menu.addAction("切换主题")
        self.__exit  = tray_menu.addAction("退出")

        system_tray.setContextMenu(tray_menu)
        system_tray.show()
        
    def set_event_bind(self):
        """设置事件绑定"""
        # 主界面
        self.__auto_checkbox.clicked.connect(self.auto_choose_theme)
        self.light_radio_btn.clicked.connect(self.set_light_theme)
        self.dark_radio_btn.clicked.connect(self.set_dark_theme)

        # 托盘 上下文菜单
        self.__change.triggered.connect(lambda: WinSystemThem().reset_windows_theme())
        self.__exit.triggered.connect(self.close)

    def auto_choose_theme(self,bool):
        """自动选择主题"""
        # 从一天中获取日出日落
        
        now  = self.one_day .get_current_time()
        sunsize = self.one_day .get_sunrise()
        sunset = self.one_day .get_sunset()

        # 选中才触发
        if bool:
            if now >= sunsize and now <= sunset:
                    WinSystemThem().set_system_theme(1)
                    self.light_radio_btn.setChecked(True)
            else:
                WinSystemThem().set_system_theme(0)    
                self.dark_radio_btn.setChecked(True)  

    def set_radio_btn_choosed(self):
        """设置选中的圆角按钮"""
        if WinSystemThem().get_system_theme():
             self.light_radio_btn.setChecked(True)
        else:
             self.dark_radio_btn.setChecked(True)

    def set_light_theme(self):
        """设置为亮色主题"""
        WinSystemThem().set_system_theme(1)

    def set_dark_theme(self):
         """设置为暗色主题"""
         WinSystemThem().set_system_theme(0)